#delimit;
clear;
set more off;

global temp /Sastemp;
global path ~;
set mem 5000m;

capture log close;
log using $path/pollution/logfiles/industries.log, replace;

/*================================================
 Program: industry_basins_2000.do
 Author : Avi Ebenstein
 Created: August 2007
=================================================*/

use ~/pollution/industries/datafiles/indtemp2000,clear;

****************************************************;
* Step 3 - Merge with DSP death rates by site       ;
****************************************************;

tostring gbcode, replace;
sort gbcode;
merge gbcode using ~/dsp/datafiles/merge_data_site;
tab _merge; 
keep if _merge==3;
drop _merge;

****************************************************;
* Step 4 - Merge with water pollution data by basin ;
****************************************************;

forvalues j=1/6{;
                sort level`j';
                merge level`j' using ~/pollution/datafiles/water_pollution_level`j';
                tab _merge;
                keep if _merge==1|_merge==3;
                capture drop _merge;
              };

gen mylevel=0;

global mylist "overall_q permanga_n a_n lead oils volatile_p bod mercury dissolved_";
foreach i of global mylist{;
                           gen `i'=0;
                           forvalues j=1/6{;
                           replace `i'=`i'_level`j' if `i'_level`j'~=.;
                           replace mylevel=`j'      if `i'_level`j'~=.;                
                                          };
                         };

label var mylevel "Precision of basin";

**************************************************;
* Step 5 - Merge with air pollution data by basin ;
**************************************************;

gen airpollution=0;
gen myairlevel=0;

forvalues j=1/6{;
                sort level`j';
                merge level`j' using ~/pollution/datafiles/air_pollution_level`j';
                tab _merge;
                keep if _merge==1|_merge==3;
                capture drop _merge;
                replace airpollution=airpollution`j' if airpollution`j'~=.;
                replace myairlevel=`j'               if airpollution`j'~=.;
              };

gen lnairpollution=ln(airpollution);

****************************************;
* Step 6 - Merge with rainfall by basin ;
****************************************;

gen rainfall=.;
forvalues i=3/6{;                
capture drop _merge;
sort level`i';
merge level`i' using ~/pollution/datafiles/rainfall_level`i';               
keep if _merge==1|_merge==3;
replace rainfall=rainfall`i' if rainfall`i'~=.;                
              };
capture drop _merge;

**************************************;
* Step 6 - Merge with output by basin ;
**************************************;

gen output=.;
gen myoutputlevel=.;

forvalues i=1/6{;                
capture drop _merge;
sort level`i' year;
merge level`i' year using ~/pollution/industries/datafiles/output_level`i'_2000_only;
keep if _merge==1|_merge==3;
replace output=output`i' if output`i'~=.;
replace myoutputlevel=`i'  if output`i'~=.;                
              };
capture drop _merge;

************************************;
* Step 7 - Merge with stream info   ;
************************************;
* a. connects DSP to waterpoints    ;
* b. connects DSP to closest stream ;
* c. connects DSP to up-down basins ;
************************************;
 
sort CNTYGB;
merge CNTYGB using ~/pollution/datafiles/riversystem_info.dta;

*************************;
* This needs to be fixed ;
*************************;

keep if _merge==1|_merge==3;

************************;
* Clean things up a bit ;
************************;

drop *_level*;
drop _merge;
capture drop dummy;
gen dummy=1;
sort dummy;

save ~/pollution/industries/datafiles/industry_basins_2000, replace;
ex;


